#include <winsock2.h>
#include "Dialog/UserSlotPanel.h"
#include "../../common/protocol.h"
#include <netlib/packet/packetpool.h>
#include "Network/NetworkManager.h"
#include "Frame/FrameChannel.h"

UserSlotPanel::UserSlotPanel() : m_enableUserInfo( false )
{
	ADD_USERFUNCTION( "ClickUserKickOut", onClickKickOut );
}

UserSlotPanel::~UserSlotPanel()
{
}

void UserSlotPanel::setParentFrameChannel( FrameChannel *channel )
{
	m_frameChannel = channel;
}

bool UserSlotPanel::create( interfacelib::Window::LoadControl *loadControl, int index )
{
	char name[ MAX_CONTROL_NAME ];
	wsprintf( name, "%s%d", SCRIPT_USER_SLOT_INFO, index );

	interfacelib::Window::LoadControl::ControlProperty *property = loadControl->getControlProperty( name );
	if ( property )
		Panel::create( property->resourceName, property->pos.x, property->pos.y, property->attribute );

	loadControl->setLinkFunctionPanel( this );
	loadControl->loadChild( SCRIPT_USER_SLOT_CONTROL, this );

	m_editUserID = ( interfacelib::Window::EditBox * )loadControl->getControl( SCRIPT_USER_SLOT_INFO_ID, interfacelib::Window::Window::WindowType::EDITBOX );
	m_windowReady = ( interfacelib::Window::Window * )loadControl->getControl( SCRIPT_USER_SLOT_INFO_READY, interfacelib::Window::Window::WindowType::WINDOW );
	
	return ( true );
}


void UserSlotPanel::enbleUserInfo( bool enableUserInfo )
{
	m_enableUserInfo = enableUserInfo;
	setReady( false );
	setChannelMaster( false );
//	m_windowIsMaster->setVisible( false );
	refresh();
}

bool UserSlotPanel::isEmptySlot()
{
	return ( !m_enableUserInfo );
}

ChannelUser *UserSlotPanel::getInfo()
{
	return ( &m_channelUser );
}

void UserSlotPanel::refresh()
{
	if ( !m_editUserID ) return;
	if ( m_enableUserInfo )
		m_editUserID->setString( m_channelUser.userID );
	else m_editUserID->clear();

}

void UserSlotPanel::setReady( bool ready )
{
	m_windowReady->setVisible( ready );
}

void UserSlotPanel::setChannelMaster( bool isMaster )
{
	m_isChannelMaster = isMaster;
//	m_windowIsMaster->setVisible( isMaster );
}

bool UserSlotPanel::isReady()
{
	return ( m_windowReady->isVisible() );
}

bool UserSlotPanel::isChannelMaster()
{
	return ( m_isChannelMaster );
//	return ( m_windowIsMaster->isVisible() );
}

void UserSlotPanel::onClickKickOut()
{
	m_frameChannel->sendKickOutReq( m_channelUser.userID );
}